#include <my_head.h>
#include "cgic.h"
#include "../cgisrc/project.h"

int msqid;

long name_code;

int has_recv = 0;

void print_html_info(char *title)
{
    printf("Content-type: text/html;charset=\"UTF-8\"\n\n");//固定格式 必须要加
	printf("<!DOCTYPE html>");
	printf("<html>");
	printf("<body>");
	printf("<center>");
	printf("<h1>%s</h1>", title);//自动跳转
	printf("</center>");
	printf("</body>");
	printf("</html>");
}
// 倒计时结束后回调执行函数
void handler(int argc)
{
	msgrcv(msqid, &msg, sizeof(msg_t) - sizeof(long), name_code, IPC_NOWAIT);
	if (0 == has_recv){
		print_html_info("网络原因导致数据不可达，请稍后重试");
		exit(0);
	}
}


int cgiMain(int argc, const char *argv[])
{
	/*获取消息队列的key值*/
	key_t my_key = ftok(MSGPATH, 'a');
	if (-1 == my_key){
		return -1;
	}
	/*获取消息队列*/
	msqid = msgget(my_key, IPC_CREAT | 0666);
	if (-1 == msqid){
		return -1;
	}

	/*注册alarm信号*/
	signal(SIGALRM, handler);

	char name[128] = {0};
	memcpy(name, cgiCookie, sizeof(name));
	char *s = name;
	while (*s != '=')
	{
		s++;
	}
	s++;
	strcpy(name, s);

	int i = 0;
	while(name[i]!='\0'){
		name_code += name[i];
		i++;
	}
	
	//printf("code：%ld\n", name_code);
	memset(&msg, 0, sizeof(msg_t));
	strcpy(msg.user.ID, name);
	/*封装消息*/
	msg.msgtype = name_code;
	msg.commd = 1; // 获取环境数据
	msgsnd(msqid, &msg, sizeof(msg_t) - sizeof(long), 0);

	alarm(5);

	memset(&msg, 0, sizeof(msg_t));
	msgrcv(msqid, &msg, sizeof(msg_t) - sizeof(long), name_code * 2, 0);
	//获取到了数据
	has_recv = 1;
	

	//设置led1-照明，风扇-温控 ，马达-湿空
	if (1 == msg.user.flags){
		printf("Content-type: text/html;charset=\"UTF-8\"\n\n");//固定格式 必须要加
		printf("<!DOCTYPE html>");
		printf("<html>");
		printf("<body bgcolor=\"#00FFFF\">");
		printf("<center>");
		printf("<table border = \"3\">");
		printf("<tr>");
		printf("<td bgcolor=\"#00FFFF\">%s</td>",name);			
		printf("</tr>");
		printf("</table>");
		printf("<h4>环境数据</h4>");
		printf("<table border = \"1\">");
		printf("<th bgcolor=\"#00FFFF\">温度</th>");
		printf("<th bgcolor=\"#00FFFF\">湿度</th>");
		printf("<th bgcolor=\"#00FFFF\">光强</th>");
		printf("<tr>");
		printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">%d</td>",msg.envdata.temp);
		printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">%d%%</td>",msg.envdata.hume);
		printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">%dlux</td>",msg.envdata.lux);
		printf("</tr>");
		printf("</table>");
		printf("<h4>设备状态</h4>");
		printf("<table border = \"1\">");
		printf("<th bgcolor=\"#00FFFF\" >照明</th>");
		printf("<th bgcolor=\"#00FFFF\">温控</th>");
		printf("<th bgcolor=\"#00FFFF\">湿控</th>");
		printf("<tr>");

		/*设备状态甄别显示*/
		if(msg.switchset.swlight != 0){
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">开</td>");
		}else{
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">关</td>");
		}
		
		if (msg.switchset.swfan != 0){
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">%d档</td>", msg.switchset.swfan);
		}else{
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">关</td>");
		}

		if(msg.switchset.swmotor != 0){
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">开</td>");			
		}else{
			printf("<td bgcolor=\"yellow\" width=\"80\" height=\"20\">关</td>");
		}

		printf("</tr>");
		printf("</table>");
		printf("<br>");
		printf("<form action=\"../cgi-bin/living_view.cgi\" method=\"get\">");
		printf("<input type=\"submit\" name=\"set_button\" value=\"点击刷新数据\" />");
		printf("</form>");
		printf("</center>");
		printf("</body>");
		printf("</html>");
	}else{
		printf("Content-type: text/html;charset=\"UTF-8\"\n\n");//固定格式 必须要加
		printf("<!DOCTYPE html>");
		printf("<html>");
		printf("<body bgcolor=\"#00FFFF\">");
		printf("<center>");
		printf("<h4>网络原因导致数据不可达，请稍后重试</h4>");
		printf("</center>");
		printf("</body>");
		printf("</html>");
	}

	return 0;
}